﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Globalization;
using System.Configuration;
using System.Xml.Serialization;
using System.Collections.Specialized;
using System.IO;
using SWCInvoice.Properties;
using SWCInvoice.Model;
using Cyotek.GhostScript;
using SWCInvoice.Utils;

namespace SWCInvoice.Servicelayer {
    public class SWCInvoiceSettings {

        private static WebConfigEncryption _webConfigEncryption = new WebConfigEncryption();
        /// <summary>
        /// Get the culture setting from the application settings
        /// </summary>
        /// <returns>CultureInfo</returns>
        public static CultureInfo GetCultureInfo() {
            return new CultureInfo(Properties.Settings.Default.Culture);

            //if (ConfigurationManager.AppSettings["Culture"].Equals("de-DE") && GetUltimateFlag().Equals("0"))
            //    return new CultureInfo("en-US");
            //else
            //    return new CultureInfo(ConfigurationManager.AppSettings["Culture"]);
        }

        /// <summary>
        /// Get the culture setting from the application settings
        /// </summary>
        /// <returns>CultureInfo</returns>
        public static string GetCulture() {
            return Properties.Settings.Default.Culture;
             //return ConfigurationManager.AppSettings["Culture"];
        }

        /// <summary>
        /// Get the temporary view path setting from the application settings
        /// </summary>
        /// <returns>TempPath</returns>
        public static string GetTemporaryViewPath() {
            return Properties.Settings.Default.TemporaryViewPath;
            //return ConfigurationManager.AppSettings["TemporaryViewPath"];
        }

        /// <summary>
        /// Get the basic result path name setting from the application settings
        /// </summary>
        /// <returns>basic result path name</returns>
        public static string GetBasicResultPathName() {
            return Properties.Settings.Default.BasicResultPathName;
            //return ConfigurationManager.AppSettings["BasicResultPathName"];
        }

        /// <summary>
        /// Get the session path name setting from the application settings
        /// </summary>
        /// <returns>session path name</returns>
        public static string GetSessionPathName() {
            return Properties.Settings.Default.SessionPathName;
            //return ConfigurationManager.AppSettings["SessionPathName"];
        }

        /// <summary>
        /// Get the merge path name setting from the application settings
        /// </summary>
        /// <returns>merge path name</returns>
        public static string GetMergePathName() {
            return Properties.Settings.Default.MergePathName;
            //return ConfigurationManager.AppSettings["MergePathName"];
        }

        /// <summary>
        /// Get the documents path name setting from the application settings
        /// </summary>
        /// <returns>documents path name</returns>
        public static string GetDocumentsPathName() {
            return Properties.Settings.Default.DocumentsPathName;
            //return ConfigurationManager.AppSettings["DocumentsPathName"];
        }

        /// <summary>
        /// Get update url flag setting from the application settings
        /// </summary>
        /// <returns>update url</returns>
        public static string GetUpdateUrlFlag() {
            return ConfigurationManager.AppSettings["UpdateUrl"];
        }

        /// <summary>
        /// Get proxy use flag setting from the application settings
        /// </summary>
        /// <returns>proxy use</returns>
        public static string GetProxyUseFlag() {
            return Properties.Settings.Default.ProxyUse.ToString();
            //return ConfigurationManager.AppSettings["ProxyUse"];
        }

        /// <summary>
        /// Get proxy flag setting from the application settings
        /// </summary>
        /// <returns>proxy</returns>
        public static string GetProxyFlag() {
            return Properties.Settings.Default.Proxy;
            //return ConfigurationManager.AppSettings["Proxy"];
        }

        /// <summary>
        /// Get domain flag setting from the application settings
        /// </summary>
        /// <returns>domain</returns>
        public static string GetDomainFlag() {
            return _webConfigEncryption.Decrypt(Properties.Settings.Default.Domain);
            //return _webConfigEncryption.Decrypt(ConfigurationManager.AppSettings["Domain"]);
        }

        /// <summary>
        /// Get user flag setting from the application settings
        /// </summary>
        /// <returns>user</returns>
        public static string GetUserFlag() {
            return _webConfigEncryption.Decrypt(Properties.Settings.Default.User);
            //return _webConfigEncryption.Decrypt(ConfigurationManager.AppSettings["User"]);
        }

        /// <summary>
        /// Get password flag setting from the application settings
        /// </summary>
        /// <returns>password</returns>
        public static string GetPasswordFlag() {
            return _webConfigEncryption.Decrypt(Properties.Settings.Default.Password);
            //return _webConfigEncryption.Decrypt(ConfigurationManager.AppSettings["Password"]);
        }

        /// <summary>
        /// Set a AppSetting Parameter
        /// </summary>
        /// <param name="settingsName">Parameter name</param>
        /// <param name="value">Value</param>
        /// <returns>true/false</returns>
        public static bool SetAppSettingParameter(string settingsName, string value) {
            try {
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings[settingsName].Value = value;
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                return true;
            } catch (Exception ex) {

                return false;
            }
        }

        /// <summary>
        /// Set a AppSetting Parameter
        /// </summary>
        /// <param name="settingsName">Parameter name</param>
        /// <param name="value">Value</param>
        /// <returns>true/false</returns>
        public static bool SetUserSettingParameter(string settingsName, string value) {
            try {
                Properties.Settings.Default.PropertyValues[settingsName].PropertyValue = value;
                Properties.Settings.Default.Save();
                return true;
            } catch (Exception ex) {

                return false;
            }
        }  
     
        /// <summary>
        /// Get the identifier settings from the application settings
        /// </summary>
        /// <returns>IdentifierCollection</returns>
        public static Identifier.IdentifierCollection GetIdentifierSettings() {

            SettingsPropertyCollection properties = Settings.Default.Properties;

            Identifier.IdentifierCollection identifiers = new Identifier.IdentifierCollection();

            foreach (SettingsProperty item in properties) {
                TextReader tr = new StringReader(item.DefaultValue.ToString());

                XmlSerializer serializer = new XmlSerializer(typeof(StringCollection));

                StringCollection c = (StringCollection)serializer.Deserialize(tr);

                identifiers.Add(Identifier.Create(c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]));
            }
            return identifiers;
        }

        /// <summary>
        /// Get the preview image format settings from the application settings
        /// </summary>
        /// <returns>the image format</returns>
        public static ImageFormat GetPreviewImageFormat() {

            string tmpImageFormat = Properties.Settings.Default.PreviewImageFormat;            
            //string tmpImageFormat = ConfigurationManager.AppSettings["PreviewImageFormat"];

            switch (tmpImageFormat) {
                case "Bitmap24":
                    return ImageFormat.Bitmap24;
                    break;
                case "Bitmap8":
                    return ImageFormat.Bitmap8;
                    break;
                case "BitmapMono":
                    return ImageFormat.BitmapMono;
                    break;
                case "Jpeg":
                    return ImageFormat.Jpeg;
                    break;
                case "Png24":
                    return ImageFormat.Png24;
                    break;
                case "Png8":
                    return ImageFormat.Png8;
                    break;
                case "PngMono":
                    return ImageFormat.PngMono;
                    break;
                case "Tiff24":
                    return ImageFormat.Tiff24;
                    break;
                case "TiffMono":
                    return ImageFormat.TiffMono;
                    break;
                default:
                    return ImageFormat.Unknown;
                    break;
            }             
        }

        /// <summary>
        /// Get the preview image antialias mode settings from the application settings
        /// </summary>
        /// <returns>the antialias mode</returns>
        public static AntiAliasMode GetPreviewImageAntiAliasMode() {

            string tmpAntiAliasMode = Properties.Settings.Default.PreviewImageAntiAliasMode;
            //string tmpAntiAliasMode = ConfigurationManager.AppSettings["PreviewImageAntiAliasMode"];

            switch (tmpAntiAliasMode) {
                case "None":
                    return AntiAliasMode.None;
                    break;
                case "Low":
                    return AntiAliasMode.Low;
                    break;
                case "Medium":
                    return AntiAliasMode.Medium;
                    break;
                case "High":
                    return AntiAliasMode.High;
                    break;               
                default:
                    return AntiAliasMode.None;
                    break;
            }            
        }

        /// <summary>
        /// Get the preview image Dpi settings from the application settings
        /// </summary>
        /// <returns>the Dpi</returns>
        public static int GetPreviewImageDpi() {
            int defaultImageDpi = 300;

            int imageDpi = int.Parse(Properties.Settings.Default.PreviewImageDpi);
            //int imageDpi = int.Parse(ConfigurationManager.AppSettings["PreviewImageDpi"]);

            if (imageDpi == null || imageDpi == 0)
                imageDpi = defaultImageDpi;

            return imageDpi;
        }
    }
}
